#include "cinput.h"
#include <QFileDialog>

#define Crypt 0
#define UnCrypt 1

CInput::CInput(QObject *parent) :
    QObject(parent)
{
    Alfavit="asdfghjklzxcvbnmqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM";
    CryptMode=1;
    WorkMode=Crypt;
    Image=0;
}

QString CInput::GetMessage()
{
    emit ProcessMassage ("Message get");
    return Message;
}
QString CInput::GetKey ()
{
    emit ProcessMassage ("Key get");
    return Key;
}
QImage CInput::GetImage()
{
    return *Image;
}
QRgb CInput::GetPixel(int x, int y)
{
    return Image->pixel (x,y);
}

bool CInput::SetMessage (QString NewMessage)
{
    Message=NewMessage;
    emit ProcessMassage ("Message set from textEdit");
    return 1;
}
bool CInput::SetKey(QString NewKey)
{
    Key=NewKey;
    emit ProcessMassage ("Key set from lineEdit");
    return 1;
}
bool CInput::SetRandomKey ()
{
    int r;
    for(int i=0;i<36;i++)
    {
        r=qrand()%Alfavit.length ();
        Key[i]=Alfavit[r];
    }
    emit ProcessMassage ("key random generated");
    return 1;
}
bool CInput::SetRandomMessage()
{
    int r;
    for(int i=0;i<500;i++)
    {
        r=qrand()%Alfavit.length ();
        Message[i]=Alfavit[r];
    }
    emit ProcessMassage ("Message random generated");
    return 1;
}
bool CInput::SetPixel(int x, int y, QRgb color)
{
    if(x<0||y<0){
        emit ProcessMassage ("Index pixel<0");
        return 0;
    }
    Image->setPixel (x,y,color);
    return 1;
}

bool CInput::SaveImage()
{
    if(Image==0){
        emit ProcessMassage ("Image is not load");
        return 0;
    }
    QString FileName=QFileDialog::getSaveFileName ();
    if(FileName==""||FileName.isEmpty ()){
        emit ProcessMassage ("FileName is empty");
        return 0;
    }
    Image->save (FileName);
    emit ProcessMassage ("Saved");
    return 1;
}
bool CInput::TurnWorkMode()
{
    if(WorkMode==Crypt){
        WorkMode=UnCrypt;
         emit ProcessMassage ("WorkMode=UnCrypt");
    }
    else{
        WorkMode=Crypt;
        emit ProcessMassage ("WorkMode=Crypt");
    }
    return 1;
}
QPixmap CInput::OpenImage ()
{
    QString FileName=QFileDialog::getOpenFileName ();
    if(FileName==0||FileName.isEmpty ()){
        emit ProcessMassage ("FileNameOpen empty");
        return QPixmap(FileName);
    }
    emit ProcessMassage ("FileName getted");
    Image=new QImage(FileName);
    emit ProcessMassage ("Image loaded");
    return QPixmap(FileName);
}
